home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / arcers / uudoall.c < prev   
Encoding:
C/C++ Source or Header  |  1994-11-08  |  7.2 KB  |  337 lines

  1. /*uudoall.c cleans up split files checks checksums and writes by h**2 7/4/91
  2.  *  (c) 1991 by Howard Helman
  3.  */
  4.  
  5.  
  6.  
  7. /*
  8.   uudoall.c can usually handle the split files with all the intervening mail
  9.   crap.
  10.   Automatically checks for checksums on lines and automatically checks for
  11.   a `size' line at the end and verifies the size.  It also computes the sum
  12.   value for the decoded file. I could not find any convention on people using
  13.   sum so the program just computes it and prints it.
  14.  
  15.   Compile by defining DOS for msdos use or UNIX for use elsewhere.
  16.  */
  17.  
  18.  
  19. /* the program is called by :
  20.     uudoall [opts] -ddir infile [...]
  21.  where opts are:
  22.         -s+             do size check
  23.         -s-             inhibit size check
  24.         -c+             do line by line checksum
  25.         -c-             inhibit checksum
  26.         -ddir   place output files in the named directory
  27.         infile  the file to process (all parts should be concatenated.)
  28.         [...]   options and file names may be repeated.
  29.                 The last option setting is in effect when a file is
  30.                 processed.
  31.  If the program cannot open the output file, it prompts the user
  32.  for a new file name or `enter' to stop.
  33.  The trimming of trailing blanks is handled properly.  This program
  34.  has been in use continually and has only had one problem: a user
  35.  signature started in col 1 and had a `M' and over sixty characters in the
  36.  line.  This is what the program searches for to find the next part of the
  37.  encoded file.
  38. */
  39.  
  40. /*
  41.   This program is donated to the public domain provided that the source
  42.   is distributed in total and my authorship of the program is credited.
  43.  
  44.         Howard Helman
  45.         Box 340
  46.         Manhattan Beach
  47.         CA 90266
  48. */
  49.  
  50. /* v1.1, 3 Oct 91   Toad Hall Tweak
  51.  * - Slight tweaks to reduce Turbo C v2.0 warnings.
  52.  * - Threw in some prototypes.
  53.  * - Reformatted to K&R standards (via good old Mister Mangler,
  54.  *   AKA indent).
  55.  * David Kirschbaum
  56.  * Toad Hall
  57.  * kirsch@usasoc.soc.mil
  58.  */
  59.  
  60.  
  61. #define UNIX
  62.  
  63. #ifdef __TURBOC__
  64. #define DOS 1
  65. #endif
  66.  
  67. #ifdef DOS
  68. #include <stdlib.h>
  69. #include <alloc.h>
  70. #define ReaD "rt"
  71. #define WritE "wb"
  72. #define SlasH "\\"
  73. #endif
  74. #ifdef UNIX
  75. #define ReaD "r"
  76. #define WritE "w"
  77. #define SlasH "/"
  78. #endif
  79. #include <stdio.h>
  80. #include <string.h>
  81. #include <ctype.h>
  82.  
  83. #define Bsize 80
  84. #define SUMSIZE 64
  85. #define DEC(c)  (((x_x=(c))>=' ')?((x_x-' ')&077):(0))  /* single char decode */
  86. #define match(x) matcher(buf,x)
  87.  
  88. static int dosize = 1, docheck = 1;
  89. static char x_x, dir[80];
  90. static long nbytes;
  91. static unsigned int sum1;
  92.  
  93. #ifdef DOS
  94. static void *vbi;
  95. static unsigned bn;
  96. #endif
  97.  
  98. #ifdef __TURBOC__
  99. /* v1.1 I like prototypes */
  100. int matcher(char *a, char *b);
  101. int hget(char *buf, int bs, FILE *in);
  102. void decode(FILE *in, FILE *out, char *dest);
  103. void doopts(char *s);
  104. void decodeit(FILE *in, char buf[Bsize]);
  105.  
  106. #endif
  107. enum States {
  108.     BeginSearch, InBody, GapOrEnd, LookEnd, PutTwo, PutEnd,
  109.     Mfind
  110. } state = BeginSearch;
  111.  
  112. int matcher(a, b)
  113. char *a, *b;
  114. {
  115.     while (*b && *a == *b)
  116.         a++, b++;
  117.     return (!*b);
  118. }
  119.  
  120. int hget(buf, bs, in)
  121. char *buf;
  122. int bs;
  123. FILE *in;
  124. {
  125.     static char s1[Bsize], s2[Bsize];
  126.  
  127.     if (state == PutEnd) {
  128.         strcpy(buf, "end");
  129.         state = BeginSearch;
  130.         return 1;
  131.     }
  132.     if (state == PutTwo) {
  133.         strcpy(buf, s2);
  134.         state = PutEnd;
  135.         return 1;
  136.     }
  137.     while (fgets(buf, bs, in)) {
  138.         switch (state) {
  139.         case BeginSearch:
  140.             if (match("begin "))
  141.                 state = InBody;
  142.             return 1;
  143.         case InBody:
  144.             if (*buf == 'M')
  145.                 return 1;
  146.             else if (*buf == '`') {
  147.                 state = BeginSearch;
  148.                 return 1;
  149.             } else {
  150.                 strcpy(s1, buf);
  151.                 state = GapOrEnd;
  152.             }
  153.             break;
  154.         case GapOrEnd:
  155.             if (match("end")) {
  156.                 strcpy(buf, s1);
  157.                 state = PutEnd;
  158.                 return 1;
  159.             } else {
  160.                 strcpy(s2, buf);
  161.                 state = LookEnd;
  162.             }
  163.             break;
  164.         case LookEnd:
  165.             if (match("end")) {
  166.                 strcpy(buf, s1);
  167.                 state = PutTwo;
  168.                 return 1;
  169.             } else
  170.                 state = Mfind;
  171.             break;
  172.         case Mfind:
  173.             if (*buf == 'M' && strlen(buf) > 60) {
  174.                 state = InBody;
  175.                 return 1;
  176.             }
  177.             break;
  178.         }
  179.     }
  180.     return 0;
  181. }
  182.  
  183. void decode(in, out, dest)
  184. FILE *in, *out;
  185. char *dest;
  186. {
  187.     int j, n, checksum, altsum;
  188.     char buf[Bsize], *bp, hold[Bsize / 4 * 3], *jp;
  189.     unsigned pcline = 0, line = 0;
  190.  
  191.     sum1 = 0;
  192.     nbytes = 0;
  193.     while (memset(buf, 0, Bsize), hget(buf, Bsize, in)
  194.       &&(n = DEC(*buf)) > 0) {
  195.         line++;
  196.         altsum = checksum = 0;
  197.         bp = buf + 1;
  198.         for (jp = hold, j = n; j > 0; j -= 3, bp += 4) {
  199.             *jp = (DEC(bp[0]) << 2) | (DEC(bp[1]) >> 4);
  200.             checksum += *jp++;
  201.             *jp = (DEC(bp[1]) << 4) | (DEC(bp[2]) >> 2);
  202.             checksum += *jp++;
  203.             *jp = (DEC(bp[2]) << 6) | (DEC(bp[3]));
  204.             checksum += *jp++;
  205.             altsum += bp[0] + bp[1] + bp[2] + bp[3];
  206.         }
  207.         for (j = 0, jp = hold; j < n; j++, jp++)
  208.             sum1 = ((sum1 >> 1) + ((sum1 & 1) ? 0x8000 : 0)
  209.                 + (*jp & 0xff)) & 0xffff;
  210.         nbytes += n;
  211.         if (fwrite(hold, 1, n, out) != n) {
  212.             fprintf(stderr, "ERROR: error writing to %s\n", dest);
  213.             exit(1);
  214.         }
  215.         if (!isspace(*bp) && docheck)
  216.             if ((checksum & (SUMSIZE - 1)) != DEC(*bp)
  217.               && (altsum & 077) != (DEC(*bp) & 077)) {
  218.                 if (!pcline)
  219.                     pcline = line;
  220.             } else if (pcline) {
  221.                 fprintf(stderr,
  222.                         "ERROR:Bad Checksum lines %u-%u\n", pcline, line);
  223.                 pcline = 0;
  224.             }
  225.     }
  226.     if (pcline)
  227.         fprintf(stderr, "ERROR:Bad Checksum lines %u-%u\n", pcline, line);
  228.     if (feof(in) ||ferror(in)) {
  229.         fprintf(stderr, "ERROR: Input ended unexpectedly!\n");
  230.         exit(1);
  231.     }
  232. }
  233.  
  234. void decodeit(in, buf)
  235. FILE *in;
  236. char buf[Bsize];
  237. {
  238.     int mode;
  239.     long filesize, tsize;
  240.     FILE *out;
  241.     char fname[128], dest[128];
  242.  
  243.     sscanf(buf, "begin %o %s", &mode, dest);
  244.     while (strcpy(fname, dir) && strcat(fname, dest)
  245.       && !(out = fopen(fname, WritE))) {
  246.         fprintf(stderr, "ERROR: Can't open output file %s\nTry new one:",
  247.                 fname);
  248.         gets(dest);
  249.         if (!*dest)
  250.             exit(1);
  251.     }
  252. #ifdef DOS
  253.     setvbuf(out, (char *) vbi + bn * 512, _IOFBF, bn * 512);
  254. #endif
  255.     fprintf(stderr, "Creating %s\n", fname);
  256.     decode(in, out, dest);
  257.     if (!hget(buf, Bsize, in) ||!match("end")) {
  258.         fprintf(stderr, "ERROR: no `end' line\n");
  259.         exit(1);
  260.     }
  261.     tsize = ftell(out);
  262.     if (dosize && hget(buf, Bsize, in) &&match("size ")) {
  263.         sscanf(buf, "size %ld", &filesize);
  264.         if (tsize != filesize) {
  265.             fprintf(stderr,
  266.             "ERROR: file should have been %ld bytes long but was %ld.\n",
  267.                     filesize, tsize);
  268.             exit(1);
  269.         }
  270.     } else if (dosize)
  271.         fprintf(stderr, "Size check not done\n");
  272.     if (tsize != nbytes) {
  273.         fprintf(stderr, "Size Error:file=%ld data=%ld\n", tsize, nbytes);
  274.         exit(1);
  275.     }
  276.     fprintf(stderr, "sums: %u %ld %ld\n", sum1, (nbytes + 1023) / 1024,
  277.             nbytes);
  278.     fclose(out);
  279. }
  280.  
  281. void doopts(s)
  282. char *s;
  283. {
  284.     if (s[1] == 's')
  285.         dosize = s[2] != '-';
  286.     else if (s[1] == 'c')
  287.         docheck = s[2] != '-';
  288.     else if (s[1] == 'd') {
  289.         strcpy(dir, s + 2);
  290.         if (*dir)
  291.             strcat(dir, SlasH);
  292.     } else {
  293.         fprintf(stderr, "Illegal flag %s\n", s);
  294.         exit(1);
  295.     }
  296. }
  297.  
  298. int main(argc, argv)
  299. int argc;
  300. char **argv;
  301. {
  302.     FILE *in;
  303.     int nf = 0, i;
  304.     char buf[80];
  305.  
  306.     *dir = 0;
  307.     if (argc < 2) {
  308.         fprintf(stderr,
  309.     "USAGE: uudoall [opts] -ddir infile [...] where opts are -s+|- -c+|-\n");
  310.         exit(1);
  311.     }
  312. #ifdef DOS
  313.     bn = coreleft() / 1024 - 2;
  314.     vbi = malloc(512 * 2 * bn);
  315. #endif
  316.     for (i = 1; i < argc; i++) {
  317.         if (argv[i][0] == '-')
  318.             doopts(argv[i]);
  319.         else if (!(in = fopen(argv[i], ReaD)))
  320.             fprintf(stderr, "ERROR: can't find %s\n", argv[i]);
  321.         else {
  322. #ifdef DOS
  323.             setvbuf(in, vbi, _IOFBF, bn * 512);
  324. #endif
  325.             while (hget(buf, Bsize, in)) {
  326.                 if (match("begin ")) {
  327.                     nf++;
  328.                     decodeit(in, buf);
  329.                 }
  330.             }
  331.             fclose(in);
  332.         }
  333.     }
  334.     return !nf;
  335. }
  336.  
  337.